package com.cheng.controller;


import com.cheng.pojo.Order;
import com.cheng.pojo.Root;
import com.cheng.pojo.User;
import com.cheng.pojo.Users;
import com.cheng.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/root")
public class RootController {

    @Autowired
    private RootService rootService;
    @Autowired
    private UserService userService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductTeamService productTeamService;



    //管理员登录
    @RequestMapping("/selectRoot")
    public String selectRoot(String username, String password, Model model, HttpSession session){
        Root root = rootService.selectRoot(username, password);
        session.setAttribute("root",root);

        if (root ==null){
            model.addAttribute("error","用户名或密码错误！");
            return "rootlogin";

        }

        return "rootframe";
    }


    //管理所有订单
    @RequestMapping(value = "/selectAllOrder")
    public String selectAllOrder(@RequestParam(value = "page", defaultValue = "1") int page,
                                     @RequestParam(value = "pageSize", defaultValue = "8") int pageSize,
                                     Model model,
                                     HttpSession session) {
        Root root= (Root) session.getAttribute("root");
        PageInfo<Order> pageInfo = rootService.selectAllOrder(page, pageSize);
        model.addAttribute("rootorders", pageInfo);
        return "rootorderlist";
    }

    //商家取消订单
    @RequestMapping("/merchantsDeleteOrder")
    public String merchantsDeleteOrder(String ordercode,String userCode,String name,String total_price,String productcode,String adult_number,String child_number){


        usersService.addMoney(name,total_price);
        userService.refund(userCode,total_price);

        productService.remove_number(productcode,adult_number,child_number);
        productTeamService.remove_number(productcode,adult_number,child_number);

        orderService.merchantsDeleteOrder(ordercode);
        return "redirect:selectAllOrder";
    }


    //查询用户
    @RequestMapping("/queryuser")
    public String queryuser(@RequestParam(value = "page", defaultValue = "1") int page,
                            @RequestParam(value = "pageSize", defaultValue = "8") int pageSize,
                            Model model,
                            HttpSession session){
        Root root= (Root) session.getAttribute("root");
        PageInfo<Users> pageInfo = rootService.queryuser(page, pageSize);
        model.addAttribute("rootusers", pageInfo);
        return "rootuserlist";

    }

    //删除用户
    @RequestMapping("/deleteuser")
    public String deleteuser(String name){

        rootService.deleteuser(name);
        return "redirect:queryuser";


    }


    //查询商家
    @RequestMapping("/queryusers")
    public String queryusers(@RequestParam(value = "page", defaultValue = "1") int page,
                            @RequestParam(value = "pageSize", defaultValue = "8") int pageSize,
                            Model model,
                            HttpSession session){
        Root root= (Root) session.getAttribute("root");
        PageInfo<User> pageInfo = rootService.queryusers(page, pageSize);
        model.addAttribute("rootusers", pageInfo);
        return "rootuserslist";

    }


    //删除用户
    @RequestMapping("/deleteusers")
    public String deleteusers(String userName){

        rootService.deleteusers(userName);
        return "redirect:queryusers";


    }






    //超级管理员退出
    @RequestMapping("/userloginOut")
    public String userloginOut(HttpSession session){
        session.removeAttribute("root");
        return "login";

    }






}
